<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>基础数据管理-项目信息新增</title>
    <script src="/scripts/boot.js"></script>
</head>
<body class="bg-white">
<div id="viewForm">
    <div class="title-info mb-20"><span>基本信息</span></div>
    <table class="table-bg" border="0" cellpadding="1" cellspacing="1" style="width:100%;table-layout:fixed;">
        <colgroup>
            <col width="135px"></col>
            <col width="50%"></col>
            <col width="135px"></col>
            <col width="50%"></col>
        </colgroup>
        <tr>
            <td class="text-titlebg">模板名称</td>
            <td colspan="3">
                <input type="text" name="name" class="mini-textbox w250" maxlength="48"  required="true" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"　vtype="maxLength:48"/>
            </td>
        </tr>
        <tr>
            <td class="text-titlebg">模板类型</td>
            <td colspan="3">
                <input type="text" id="type" name="type" class="mini-combobox w250"  required="true" errorMode="none" onvalidation="onChoseValidation" requiredErrorText="请选择"　 url="/dictionary/listValues?name=templete_type" idField="id" textField="value" onvaluechanged="viewProfessionType"/>
            </td>
        </tr>
         <tr style="display:none" id="zhuan">
            <td class="text-titlebg">业务类型</td>
            <td colspan="3">
                <input type="text" name="professionalType" class="mini-combobox w250" required="true" errorMode="none" onvalidation="onChoseValidation" requiredErrorText="请选择"　 url="/businessType/listAll" idField="id" textField="typeName" onvaluechanged=""/>
            </td>
        </tr>
        <tr>
            <td class="text-titlebg noEmpty">备注</td>
            <td class="wp" colspan="3">
                <textarea name="remark" class="mini-textarea wtextarea" emptyText="" maxLength="500"></textarea>
            </td>
        </tr>
    </table>
    <div class="flex_div">
       	<div class="flex_box">
       		<div class="flex_title">文件</div>
       		<div class="flex-filebox">
       			<!-- <div class='flex-file'>
	    			<a class='flex-filename'>name</a>
	    			<span class='img-wh'></span>
   				</div> -->
       		</div>
       	</div>
       	<div class="flex_up"><a class="up-botton" onclick="view.upload()"><span class="add-bg"></span>上传</a></div>
    </div>
    <div class="mini-fit"></div>
    <div class="chose-toolbar">
        <a class="chose-button" onclick="CloseWindow('close')">取消</a>
        <a class="chose-button save-button" onclick="view.save()">保存</a>
    </div>
</div>
<script src="/scripts/res/js/common.js"></script>
<script type="text/javascript">
	function viewProfessionType(){
		if(mini.get("type").getSelected().code == "100006"){
			$("#zhuan").show();
		}else if(mini.get("type").getSelected().code != "100006"){
			$("#zhuan").hide();
		}
	};
    var post_flag,form,state,templeteFile;
    var view = {
        init: function () {
            post_flag = false; //设置一个对象来控制是否进入AJAX过程
            templeteFile = "";
            mini.parse();
            form = new mini.Form("#viewForm");
        },
        //保存
        save: function () {
            form.validate();
            if (form.isValid() == false) return;
            if (post_flag) return;
            if (templeteFile==""){
                common.prompt("请上传模板文件");
                return;
            }
           
            var data = form.getData();//获取表单多个控件的数据
        	common.loading("加载中。。。");
            // data.file = templeteFile;
           	templeteFile.append("type.id",data.type);
            templeteFile.append("professionalType.id",data.professionalType);
            templeteFile.append("name", data.name);
            templeteFile.append("remark", data.remark);
            

            // data.templeteUrl = templeteUrl;
            // data.templeteFileName = templeteFileName;
            post_flag=true
            $.ajax({
                url: (state=="new"?"/planTemplete/save":"/planTemplete/edit?id=" + data.id),
                type: "post",
                enctype: 'multipart/form-data',
                data: templeteFile,
                processData: false,
                contentType: false,
                cache: false,
                timeout: 600000,
                async:true,
                success: function (data) {
                	common.unmask();
                	if(!data.state){
                		templeteFile.delete("name")
                		templeteFile.delete("remark")
                		templeteFile.delete("type.id")
                		templeteFile.delete("professionalType.id")

                		post_flag=false;
                        mini.alert(data.message);
                	}else{
                         CloseWindow("ok")
                	}
                   
                },
                error: function (errorMsg) {
                    post_flag = false;

                }
            });
        },
//    数据展示
        SetData: function (data) {
            data = mini.clone(data);
            state = data.state;
            if(data.state=="edit"){
                $.ajax({
                    url:"/planTemplete/info?id=" + data.id,
                    type: "post",
                    success: function (text) {
                        //跨页面传递的数据对象，克隆后才可以安全使用
                        var datas = mini.decode(text);   //反序列化成对象
                        var fileName = datas.fileEntities[0].fileName;
                        var fileUrl = datas.fileEntities[0].fileUrl;
                        var fileid = datas.fileEntities[0].id;
                        form.setData(datas);             //设置多个控件数据
                        templeteFileName =fileName;
                        templeteUrl = fileUrl;
                        var fileBox = "<div class='flex-file'>"+
                            "<a class='flex-filename' onclick=\"common.downFile('" +fileUrl+"','"+fileName+"')\">"+fileName+"</a>"+
                            "</div>";
                        $(".flex-filebox").append(fileBox);
                        common.unmask();
                    }
                });
            }else{
                $('input[name=id]').val("");
            }
        },
        upload: function(){
        	//附件上传
			view.uploadFilesThis({
				config : {
					// accept : 'image/png,image/jpg,image/jpeg,image/gif,.xls,.doc,.txt,.pdf',
					accept : '.xls',
					multiple : false,
                    sync:true,//是否展示数据
				},
				success : function(data){
                    //获取附件
                    templeteFile =data;
				}
			});
        },
        uploadFilesThis : function(params){
            var accept = params.config.accept?params.config.accept:'';
            var multiple = params.config.multiple;
            var container = params.config.container;
            var sync = (params.config.sync==false)?params.config.sync:true;
            var form = $('<form name="fileForm" id="fileForm" method="post" enctype="multipart/form-data"></form>').css({visibility:'hidden'});
            var file = $('<input type="file" name="file" class="file" id="file" />');
            if(multiple){
                file.attr('multiple', 'multiple');
            }
            form.append(file).appendTo('body');
            if(accept){
                file.attr('accept', accept);
            }
            file[0].addEventListener("change", function(){
                if(sync){
                    var str = $(this).val();
                    var fileName = view.getFileName(str);
                    var fileExt = str.substring(str.lastIndexOf('.') + 1);
                    $(".flex-filebox").empty();
                    var fileBox = "<div class='flex-file'>"+
                    		"<a class='flex-filename'>"+fileName+"</a>"+
                    		/*"<span class='img-wh'></span>"+*/
                    		"</div>";
                    $(".flex-filebox").append(fileBox);
                    var forms = form[0];
                    var data = new FormData(forms);
                    var files = document.querySelector('[type=file]');
                    data.append('file', files.files[0]);
                    params.success(data);
                }
            }, false);
            file.click();
            return form[0];
        },
        //	获取文件名称
        getFileName :function(path){
            var pos1 = path.lastIndexOf('/');
            var pos2 = path.lastIndexOf('\\');
            var pos = Math.max(pos1, pos2);
            if (pos < 0) {
                return path;
            }
            else {
                return path.substring(pos + 1);
            }
        }
    };
    view.init();
</script>
</body>
</html>